<?php


namespace app\crontab\controller\once;


use custom\Curl;
use house\HouseCount1;
use custom\Log;
use custom\Mongo;
use custom\Redis;
use house\HouseCount2;
use house\HouseCount3;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Exception;

class MyTest extends Command {


    protected function configure() {
        $this->setName('MyTest');
    }

    protected function execute(Input $input, Output $output) {
        $this->d();
    }

    public function a() {
        $list = db('house')
            ->field('city_code,city_codes,id')
            ->select();
        $arr = [];
        foreach ($list as $index => $item) {
            $a = explode(',', $item['city_codes']);
            if (isset($a[3]) && $a[3] != $item['city_code']) {
                $arr[] = $item['id'];
//                db('house')
//                    ->where('id', $item['id'])
//                    ->update(['city_code' => $a[3]]);
            }
        }
        Log::write($arr, 'MyTest.log');
    }

    public function b() {
        set_time_limit(0);
        echo "start\n";
        $options = [
            'sort' => ['_id' => 1],
        ];
        $config = config('db.mongo2');
        Mongo::setOption($config);
        $mongo = Mongo::getInstance();
        $cursor = $mongo->select('SchoolPrimaryVillage', [], $options);
        foreach ($cursor as $index => $item) {
            $api_host = HouseCount1::API_URL_HOST['成都市'];
            $kw = urlencode($item->v_name);
            $city_id = urlencode($api_host['city_id']);
            $api_url = "{$api_host['url']}esf-ajax/community/pc/autocomplete?city_id={$city_id}&kw={$kw}";
            $ajk_village = xhr_proxy($api_url, 'GET');
            $result = json_decode($ajk_village, true);
            if ($result['status'] == "ok" && !empty($result['data'])) $name = replaceSpecialChar($result['data'][0]['name']);
            else $name = '';
            $mongo->update('SchoolPrimaryVillage', ['_id' => $item->_id], ['ajk_name' => $name]);
        }
        echo "end\n";
    }

    public function c() {
        set_time_limit(0);
        echo "start\n";
        $options = [
            'sort' => ['_id' => 1],
        ];
        $config = config('db.mongo2');
        Mongo::setOption($config);
        $mongo = Mongo::getInstance();
        $cursor = $mongo->select('SchoolPrimaryVillage', ['_id' => ['$gt' => 4031]], $options);
        foreach ($cursor as $index => $item) {
            $api_host = HouseCount1::API_URL_HOST['成都市'];
            $kw = urlencode($item->v_name);
            $city_id = urlencode($api_host['city_id']);
            $api_url = "{$api_host['url']}ajax/autocomplete?city_id={$city_id}&kw={$kw}";
            $ajk_village = xhr_proxy($api_url, 'GET');
            $result = json_decode($ajk_village, true);
            if ($result['code'] == 0 && !empty($result['data'])) {
                $name = replaceSpecialChar($result['data'][0]['keyword']);
            } else {
                $name = '';
            }
            $mongo->update('SchoolPrimaryVillage', ['_id' => $item->_id], ['ajk_name_home_page' => $name]);
        }
        echo "end\n";
    }

    public function d() {
    }

}